package com.example.wujinapi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.wujinapi.dao.FavouritesDao;
import com.example.wujinapi.entity.Favourites;
import com.example.wujinapi.service.FavouritesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * (Favourites)表服务实现类
 *
 * @author makejava
 * @since 2023-07-10 22:36:08
 */
@Service("favouritesService")
public class FavouritesServiceImpl extends ServiceImpl<FavouritesDao, Favourites> implements FavouritesService {

    @Autowired
    private FavouritesDao favouritesDao;

    @Override
    public void addFavourite(Favourites favourites) {
        Favourites fvData=favouritesDao.selectOne(new QueryWrapper<Favourites>().eq("userid",favourites.getUserid()).eq("pid", favourites.getPid()));
        if(fvData==null){ //为空才插入新用户
            favouritesDao.insert(favourites);
        }
    }

    @Override
    public void deleteFavourite(int favId) {
        favouritesDao.delete(new QueryWrapper<Favourites>().eq("id",favId));
    }

    @Override
    public List<Map<String, Object>> getFavourites(int userid,int pageNum, int pageSize) {
        Page<Favourites> page = new Page<Favourites> (pageNum,pageSize); //实现定义的接口
        List<Map<String,Object>> Datas=favouritesDao.selectPageVo(userid,page);
        return Datas;
    }
}

